package com.java.controller;

import com.java.model.MediaAccount;
import com.java.model.Price;
import com.java.model.Result;
import com.java.service.MediaAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/mediaAccountLogin")
public class MediaAccountLoginController {

    @Autowired
    private MediaAccountService mediaAccountService;

    /**
     * 跳转自媒体注册
     * @return
     */
    @GetMapping("/login")
    public String MediaAccountLogin() {
        return "/MediaAccount/mediaLogin";
    }

    /**
     * 自媒体注册
     * @param mediaAccount
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Result save(MediaAccount mediaAccount) {
        return mediaAccountService.save(mediaAccount);
    }

    /**
     * 跳转自媒体基本资料
     */
    @GetMapping("/basicData")
    public String basicData(HttpServletRequest request, Model model) {
        MediaAccount user = (MediaAccount) request.getSession().getAttribute("user");
        model.addAttribute("mediaAccount", user);
        return "/user/user-setting";
    }

    /**
     * 跳转自媒体报价
     */
    @GetMapping("/quotation")
    public String quotation(HttpServletRequest request, Model model) {
        MediaAccount user = (MediaAccount) request.getSession().getAttribute("user");
        Result result = mediaAccountService.priceId(user.getPrice().getId());

        model.addAttribute("mediaAccount", user);
        if (result.getCode().equals("0")) {
            model.addAttribute("price", result.getData());
        }

        return "/user/user-quotation";
    }

    /**
     * 修改自媒体的报价表
     * @return
     */
    @PostMapping("/quotationUpdate")
    @ResponseBody
    public Result quotationUpdate(Price price) {
        return mediaAccountService.priceUpdate(price);
    }


    /**
     * 跳转自媒体修改密码
     */
    @GetMapping("/password")
    public String password(HttpServletRequest request, Model model) {
        MediaAccount user = (MediaAccount) request.getSession().getAttribute("user");
        model.addAttribute("mediaAccount", user);
        return "/user/user-password";
    }

    /**
     * 修改自媒体密码
     * @return
     */
    @PostMapping("/basicDateTest")
    @ResponseBody
    public Result basicDateTest(MediaAccount mediaAccount) {
        return mediaAccountService.passwordSave(mediaAccount);
    }
}
